package elements.axis; 
import flash.text.TextField;
import flash.display.Sprite;
import flash.text.TextFormat;
import string.Utils;

class RadarAxisLabels extends Sprite {

	var style:Dynamic;
	public var labels:Array<Dynamic>;
	
	
	public function new( json:Dynamic ) {
		super();
		// default values
		this.style = {
			colour:			'#784016',
			steps:			1
		};
		
		if( json != null )
			Object_helper.merge_2( json, this.style );
			
		this.style.colour = Utils.get_colour( this.style.colour );
		
		// cache the text for tooltips
		this.labels = new Array<String>();
		var values:Array<Dynamic>;
		var ok:Bool = false;
		
		if( ( Std.is( this.style.labels, Array) ) && ( this.style.labels.length > 0 ) )
		{
			
			for (s in this.style.labels )
				this.add( s, this.style );
		}
		
	}

	public function add( label:Dynamic, style:Dynamic ) : Void
	{
		var label_style:Dynamic = {
			colour:		style.colour,
			text:		'',
			size:		style.size,
			visible:	true
		};

		if( Std.is( label, String) )
			label_style.text = cast( label, String);
		else {
			Object_helper.merge_2( label, label_style );
		}

		// our parent colour is a number, but
		// we may have our own colour:
		if( Std.is( label_style.colour, String) )
			label_style.colour = Utils.get_colour( label_style.colour );

		this.labels.push( label_style.text );

		//
		// inheriting the 'visible' attribute
		// is complext due to the 'steps' value
		// only some labels will be visible
		//
		if( label_style.visible == null )
		{
			//
			// some labels will be invisible due to our parents step value
			//
			if ( ( (this.labels.length - 1) % style.steps ) == 0 )
				label_style.visible = true;
			else
				label_style.visible = false;
		}

		var l:TextField = this.make_label( label_style );
		this.addChild( l );
	}
	
	public function make_label( label_style:Dynamic ):TextField {
		
		// we create the text in its own movie clip
		
		var tf:TextField = new TextField();
	tf.x = 0;
		tf.y = 0;
		tf.text = label_style.text;
		
		var fmt:TextFormat = new TextFormat();
		fmt.color = label_style.colour;
		fmt.font = "Verdana";
		fmt.size = label_style.size;
		fmt.align = "right";
		tf.setTextFormat(fmt);
		
		tf.autoSize = "left";
		tf.visible = label_style.visible;
		return tf;
	}
	
	// move y axis labels to the correct x pos
	public function resize( sc:ScreenCoordsRadar ):Void {

		var i:Float;
		var tf:TextField;
		var center:Int = sc.get_center_x();
		
		for( i in 0...this.numChildren) {
			// right align
			tf = cast( this.getChildAt(i), TextField);
			tf.x = center - tf.width;
		}
		
		// now move it to the correct Y, vertical center align
		for ( i in 0...this.numChildren) {
			
			tf = cast( this.getChildAt(i), TextField);
			tf.y = ( sc.get_y_from_val( i, false ) - (tf.height / 2) );
		}
	}
	
	public function die(): Void {
		
		this.style = null;
		this.labels = null;
		
		this.graphics.clear();
		while ( this.numChildren > 0 )
			this.removeChildAt(0);
	}
}
